package controller.impl;

import java.util.ArrayList;
import java.util.List;
import util.page.PageBean;

import model.bo.Article;
import model.bo.Articletype;
import model.bo.Contentus;
import model.bo.Dtable;
import model.bo.Manager;
import model.bo.SubTree;
import model.dao.IArticleDAO;
import model.dao.IArticletypeDAO;
import model.dao.IContentusDAO;
import model.dao.IDtableDAO;
import model.dao.IManagerDAO;
import controller.services.IManagerServices;


public class ManagerServices implements IManagerServices {
	//定义操作接口
	private IManagerDAO IManagerDAO;
	private IArticleDAO IArticleDAO;
	//联系我们
	private IContentusDAO IContentusDAO;
	private IArticletypeDAO IArticletypeDAO;
	
	private IDtableDAO IDtableDAO;

	/* (non-Javadoc)
	 * @see controller.impl.IManagerServices#getIManagerDAO()
	 */
	public IManagerDAO getIManagerDAO() {
		return IManagerDAO;
	}

	/* (non-Javadoc)
	 * @see controller.impl.IManagerServices#setIManagerDAO(model.dao.IManagerDAO)
	 */
	public void setIManagerDAO(IManagerDAO managerDAO) {
		IManagerDAO = managerDAO;
	}

	/* (non-Javadoc)
	 * @see controller.impl.IManagerServices#attachClean(model.bo.Manager)
	 */
	public void attachClean(Manager instance) {
		// TODO Auto-generated method stub
		
	}

	/* (non-Javadoc)
	 * @see controller.impl.IManagerServices#attachDirty(model.bo.Manager)
	 */
	public void attachDirty(Manager instance) {
		// TODO Auto-generated method stub
		
	}

	/* (non-Javadoc)
	 * @see controller.impl.IManagerServices#delete(model.bo.Manager)
	 */
	public void delete(Manager persistentInstance) {
		// TODO Auto-generated method stub
		
	}
	
	//执行SQL删除
	public Boolean delete(String arr) {
		try{
			String sql = "delete from article where articleid in ("+arr+")";
			IArticleDAO.execusql(sql);
			return true;
		}catch(Exception e){
			e.printStackTrace();
		}
		return false;
	}

	/* (non-Javadoc)
	 * @see controller.impl.IManagerServices#findAll()
	 */
	public List findAll() {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see controller.impl.IManagerServices#findByAdminname(java.lang.Object)
	 */
	public List findByAdminname(Object adminname) {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see controller.impl.IManagerServices#findByAdminpass(java.lang.Object)
	 */
	public List findByAdminpass(Object adminpass) {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see controller.impl.IManagerServices#findByExample(model.bo.Manager)
	 */
	public List findByExample(Manager instance) {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see controller.impl.IManagerServices#findById(java.lang.Integer)
	 */
	public Manager findById(Integer id) {
		// TODO Auto-generated method stub
		return IManagerDAO.findById(id);
	}

	/* (non-Javadoc)
	 * @see controller.impl.IManagerServices#findByLoginip(java.lang.Object)
	 */
	public List findByLoginip(Object loginip) {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see controller.impl.IManagerServices#findByProperty(java.lang.String, java.lang.Object)
	 */
	public List findByProperty(String propertyName, Object value) {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see controller.impl.IManagerServices#merge(model.bo.Manager)
	 */
	public Manager merge(Manager detachedInstance) {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see controller.impl.IManagerServices#save(model.bo.Manager)
	 */
	public void save(Manager transientInstance) {
		// TODO Auto-generated method stub
		IManagerDAO.save(transientInstance);
	}

	/* (non-Javadoc)
	 * @see controller.impl.IManagerServices#checkuser(java.lang.String, java.lang.String)
	 */
	public Boolean checkuser(String name, String password) {
		// TODO Auto-generated method stub
		if(IManagerDAO.findByAdminname(name).size()>0){
			//存在这个用户
			Manager admin = new Manager();
			//取出第一个进行对比
			admin = (Manager) IManagerDAO.findByAdminname(name).get(0);
			String adminPass = admin.getAdminpass();
			if(util.MD5.MD5Password(password).equals(adminPass)){
				//登陆成功
				return true;
			}
		}
		return false;
	}
	
	//查询属性带分页
	/* (non-Javadoc)
	 * @see controller.impl.IManagerServices#findByProperty(java.lang.String, java.lang.Object, int, int)
	 */
	public PageBean findByProperty(String propertyName, Object value,int pageSize,int page) {
		// TODO Auto-generated method stub
		String hql = "from Article as model where model.typeid like '%"+value+"%'";
		int allRow = IArticleDAO.getAllRowCount(hql);
		int totalPage = PageBean.countTotalPage(pageSize, allRow);    //总页数
        final int offset = PageBean.countOffset(pageSize, page);    //当前页开始记录
        final int length = pageSize;    //每页记录数
        final int currentPage = PageBean.countCurrentPage(page);
        List articlelist = IArticleDAO.queryForPage(hql,offset, length);
        PageBean pageBean = new PageBean();
        pageBean.setPageSize(pageSize);    
        pageBean.setCurrentPage(currentPage);
        pageBean.setAllRow(allRow);
        pageBean.setTotalPage(totalPage);
        pageBean.setList(articlelist);
        pageBean.init();
        return pageBean;
	}
	
	//通过类型查找
	public PageBean articlelist(String typeid,int pageSize,int page) {
		String hql = "from Article as model where model.typeid = "+typeid;
		int allRow = IArticleDAO.getAllRowCount(hql);
		int totalPage = PageBean.countTotalPage(pageSize, allRow);    //总页数
        final int offset = PageBean.countOffset(pageSize, page);    //当前页开始记录
        final int length = pageSize;    //每页记录数
        final int currentPage = PageBean.countCurrentPage(page);
        List articlelist = IArticleDAO.queryForPage(hql,offset, length);
        PageBean pageBean = new PageBean();
        pageBean.setPageSize(pageSize);    
        pageBean.setCurrentPage(currentPage);
        pageBean.setAllRow(allRow);
        pageBean.setTotalPage(totalPage);
        pageBean.setList(articlelist);
        pageBean.init();
        return pageBean;
	}
	
	//通过父类型查找
	public PageBean articlelistByFatherID(String fatherid,int pageSize,int page) {
		String hql = "from Article as model where model.fathertypeid = "+fatherid;
		int allRow = IArticleDAO.getAllRowCount(hql);
		int totalPage = PageBean.countTotalPage(pageSize, allRow);    //总页数
        final int offset = PageBean.countOffset(pageSize, page);    //当前页开始记录
        final int length = pageSize;    //每页记录数
        final int currentPage = PageBean.countCurrentPage(page);
        List articlelist = IArticleDAO.queryForPage(hql,offset, length);
        PageBean pageBean = new PageBean();
        pageBean.setPageSize(pageSize);    
        pageBean.setCurrentPage(currentPage);
        pageBean.setAllRow(allRow);
        pageBean.setTotalPage(totalPage);
        pageBean.setList(articlelist);
        pageBean.init();
        return pageBean;
	}
	
	/* (non-Javadoc)
	 * @see controller.impl.IManagerServices#articlelist(java.lang.String, java.lang.String, int, int)
	 */
	public PageBean articlelist(String typeid,String condetions,int pageSize,int page){
		String hql = "from Article as model where model.typeid=="+typeid+" and model.title like '%"+condetions+"%'";
		int allRow = IArticleDAO.getAllRowCount(hql);
		int totalPage = PageBean.countTotalPage(pageSize, allRow);    //总页数
        final int offset = PageBean.countOffset(pageSize, page);    //当前页开始记录
        final int length = pageSize;    //每页记录数
        final int currentPage = PageBean.countCurrentPage(page);
        List articlelist = IArticleDAO.queryForPage(hql,offset, length);
        PageBean pageBean = new PageBean();
        pageBean.setPageSize(pageSize);    
        pageBean.setCurrentPage(currentPage);
        pageBean.setAllRow(allRow);
        pageBean.setTotalPage(totalPage);
        pageBean.setList(articlelist);
        pageBean.init();
        return pageBean;
	}

	//保存文章
	/* (non-Javadoc)
	 * @see controller.impl.IManagerServices#save(model.bo.Article)
	 */
	public Boolean save(Article transientInstance){
		try{
			IArticleDAO.save(transientInstance);
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return false;
	}
	
	//返回第一条记录
	public Contentus getContentus(){
		Contentus c = new Contentus();
		c = IContentusDAO.findById(new Integer(1));
		return c;
	}
	//更新联系我们的记录
	public Boolean updateContentus(String contentValue){
		try{
			Contentus contentus = new Contentus();
			contentus.setId(1);
			contentus.setContentitle("-");
			contentus.setContentvalue(contentValue);
			IContentusDAO.save(contentus);
			return true;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return false;
	}
	//查询单篇文章
	public Article getArticleById(Integer id) {
		// TODO Auto-generated method stub
		return IArticleDAO.findById(id);
	}
	
	//获得子父类树
	public List<SubTree> getTypetree() {
		List<Articletype> allType = IArticletypeDAO.findAll();
		List<SubTree> result = new ArrayList<SubTree>();
		//把根目录填充节点
		for(int i=0;i<allType.size();i++){
			//判断父子分类
			if(allType.get(i).getFatherid()==0){
				List<Articletype> _temp_ = new ArrayList<Articletype>();
				//定义变量保存对象
				Articletype artype = allType.get(i);
				//保存父ID
				int fid = artype.getId();
				SubTree tree = new SubTree();
				tree.setId(artype.getId());
				tree.setNodeName(artype.getTypename());
				//通过父ID去匹配子类ID；
				for(int j=0;j<allType.size();j++){
					if(fid==allType.get(j).getFatherid()){
						Articletype childNode = (Articletype)allType.get(j);
						_temp_.add(childNode);
					}
				}
				//初始化节点对象
				tree.setHasChild(_temp_);
				//添加进入节点
				result.add(tree);
			}
		}
		return result;
	}
	
	//单独获得首页的分类  <---- 这个地方可以使用方法重载，暂时先这样。需要修复。
	public List<SubTree> getIndexType() {
		List<Articletype> allType = IArticletypeDAO.findAll();
		List<SubTree> result = new ArrayList<SubTree>();
		//把根目录填充节点
		for(int i=0;i<allType.size();i++){
			//判断父子分类
			if(allType.get(i).getFatherid()==100){
				List<Articletype> _temp_ = new ArrayList<Articletype>();
				//定义变量保存对象
				Articletype artype = allType.get(i);
				//保存父ID
				int fid = artype.getId();
				SubTree tree = new SubTree();
				tree.setId(artype.getId());
				tree.setNodeName(artype.getTypename());
				//通过父ID去匹配子类ID；
				for(int j=0;j<allType.size();j++){
					if(fid==allType.get(j).getFatherid()){
						Articletype childNode = (Articletype)allType.get(j);
						_temp_.add(childNode);
					}
				}
				//初始化节点对象
				tree.setHasChild(_temp_);
				//添加进入节点
				result.add(tree);
			}
		}
		return result;
	}
	
	//获得所有文章分类
	public List<Articletype> getAllArticleTypes() {
		List<Articletype> allType = IArticletypeDAO.findAll();
		return allType;
	}
	
	//通过父ID查询出子ID
	public List<Articletype> getChild(Integer fatherid){
		List<Articletype> allchild = IArticletypeDAO.findByFatherid(fatherid);
		return allchild;
	} 
	//通过ID查找
	public Dtable findTableById(Integer id){
		return IDtableDAO.findById(id);
	}
	
	//通过表类型查找
	/*
	 * 0商标表格
	 * 1专利表格
	 * 2版权表格
	 * 3知识产权自主优惠政策专栏
	 */
	public List findTableByType(Integer typeid){
		return IDtableDAO.findByTalbetype(typeid);
	}
	
	//查询出所有的表格，部分类型
	public PageBean dtablelist(int pageSize,int page){
		String hql = "from Dtable";
		int allRow = IDtableDAO.getAllRowCount(hql);
		int totalPage = PageBean.countTotalPage(pageSize, allRow);    //总页数
        final int offset = PageBean.countOffset(pageSize, page);    //当前页开始记录
        final int length = pageSize;    //每页记录数
        final int currentPage = PageBean.countCurrentPage(page);
        List articlelist = IDtableDAO.queryForPage(hql,offset, length);
        PageBean pageBean = new PageBean();
        pageBean.setPageSize(pageSize);    
        pageBean.setCurrentPage(currentPage);
        pageBean.setAllRow(allRow);
        pageBean.setTotalPage(totalPage);
        pageBean.setList(articlelist);
        pageBean.init();
        return pageBean;
	}
	
	//通过表格类型查找
	public PageBean dtablelist(String typeid,int pageSize,int page){
		String hql = "from Dtable as model where model.talbetype="+typeid;
		int allRow = IDtableDAO.getAllRowCount(hql);
		int totalPage = PageBean.countTotalPage(pageSize, allRow);    //总页数
        final int offset = PageBean.countOffset(pageSize, page);    //当前页开始记录
        final int length = pageSize;    //每页记录数
        final int currentPage = PageBean.countCurrentPage(page);
        List articlelist = IDtableDAO.queryForPage(hql,offset, length);
        PageBean pageBean = new PageBean();
        pageBean.setPageSize(pageSize);    
        pageBean.setCurrentPage(currentPage);
        pageBean.setAllRow(allRow);
        pageBean.setTotalPage(totalPage);
        pageBean.setList(articlelist);
        pageBean.init();
        return pageBean;
	}
	
	//查询出所有的表格，部分类型
	public List getAllTable(){
		List tableLIst = IDtableDAO.findAll();
        return tableLIst;
	}
	
	//保存或更新表格
	public void createOrUpdate(Dtable transientInstance){
		IDtableDAO.save(transientInstance);
	}
	
	//删除表格
	public Boolean deletetalbe(Integer tableId){
		try{
			IDtableDAO.delete(IDtableDAO.findById(tableId));
			return true;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return false;
	}
	

	public Articletype getArticleTypeById(String typeid) {
		// TODO Auto-generated method stub
		return IArticletypeDAO.findById(new Integer(typeid));
	}

	/* (non-Javadoc)
	 * @see controller.impl.IManagerServices#getIArticleDAO()
	 */
	public IArticleDAO getIArticleDAO() {
		return IArticleDAO;
	}

	/* (non-Javadoc)
	 * @see controller.impl.IManagerServices#setIArticleDAO(model.dao.IArticleDAO)
	 */
	public void setIArticleDAO(IArticleDAO articleDAO) {
		IArticleDAO = articleDAO;
	}

	public IContentusDAO getIContentusDAO() {
		return IContentusDAO;
	}

	public void setIContentusDAO(IContentusDAO contentusDAO) {
		IContentusDAO = contentusDAO;
	}

	public IArticletypeDAO getIArticletypeDAO() {
		return IArticletypeDAO;
	}

	public void setIArticletypeDAO(IArticletypeDAO articletypeDAO) {
		IArticletypeDAO = articletypeDAO;
	}

	public IDtableDAO getIDtableDAO() {
		return IDtableDAO;
	}

	public void setIDtableDAO(IDtableDAO dtableDAO) {
		IDtableDAO = dtableDAO;
	}
}